package com.tanhua.dubbo.utils;

import com.tanhua.model.mogo.Friend;
import com.tanhua.model.mogo.Movement;
import com.tanhua.model.mogo.MovementTimeLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TimeLineService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Async
    public void SaveMovementTimeLine(Long userId,Movement movement) {
        Criteria criteria = new Criteria();
        criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        //将好友id存到好友时间线表中
        for (Friend friend : friends) {
            MovementTimeLine movementTimeLine = new MovementTimeLine();
            movementTimeLine.setMovementId(movement.getId());//动态id
            movementTimeLine.setFriendId(friend.getFriendId());//好友id
            movementTimeLine.setCreated(System.currentTimeMillis());//当前时间
            movementTimeLine.setUserId(movement.getUserId());//用户id
            mongoTemplate.save(movementTimeLine);
        }
    }
}
